﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace VBAConvertor.Parsers
{
    public class MeReplacementParser : ILineParser
    {
        private static readonly Regex MeOp =
            new Regex(
                @"(^| )Me\.",
                RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.Singleline);

        public string Parse(Stack<ParseStatus> statuses, string lineContent, LinesReader reader, out bool success, ref bool lineClosed)
        {
            success = MeOp.IsMatch(lineContent);
            if (!success) return lineContent;
            return MeOp.Replace(lineContent, match =>
                {
                    if (match.Success)
                    {
                        return match.Value.Replace("Me", "this");
                    }
                    throw new Exception();
                });
        }
    }
}
